package parsing.google;

import core.objects.BaseObject;
import core.objects.Book;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ResponseParser {

	private static final String JSON_ITEMS = "items";
	private static final String JSON_VOLUME_INFO = "volumeInfo";
	private static final String JSON_TITLE = "title";
	private static final String JSON_AUTHORS = "authors";
	private static final String JSON_PUBLISHER = "publisher";
	private static final String JSON_PUBLISHED_DATE = "publishedDate";
	private static final String JSON_DESCRIPTION = "description";
	private static final String JSON_PAGE_COUNT = "pageCount";
	private static final String JSON_CATEGORIES = "categories";
	private static final String JSON_AVG_RATING = "averageRating";
	private static final String JSON_IMAGRE_LINKS = "imageLinks";
	private static final String JSON_THUMBNAIL = "thumbnail";
	private static final String JSON_LANGUAGE = "language";
	
	
	public static BaseObject parseJSONResponse(String response) throws MalformedURLException {
		
		Book book = null;
		try {
			
			JSONObject json = new JSONObject(response);
			
			if (!json.has(JSON_ITEMS))
				return null;
			
			JSONArray items = json.getJSONArray(JSON_ITEMS);
			if (items.length() == 0)
				return null;
			
			JSONObject volumeInfo = items.getJSONObject(0).getJSONObject(JSON_VOLUME_INFO);
			
			String title = volumeInfo.getString(JSON_TITLE);
			//this should be given as parameter from database, not created here!!!:
			book = new Book(0, title);
			
			System.out.println(title);
			System.out.println("-----------------------------");
			
			if (volumeInfo.has(JSON_AUTHORS)) {
				JSONArray authorsArray = volumeInfo.getJSONArray(JSON_AUTHORS);
				int authors_count = authorsArray.length();
				for (int i = 0; i < authors_count; i++) {
					//go to db with author and check is exists.
					//if yes, if book.getAuthors().contains() do nothing
					//        otherwise, add
					//if not, create new Author and attack to book
					System.out.println("Author: " + authorsArray.getString(i));
				}
			}
			
			if (volumeInfo.has(JSON_PUBLISHER)) {
				String publisher = volumeInfo.getString(JSON_PUBLISHER);
				//check with db if publisher exists. if so, if not attached to book, attach it.
				//If not, create new publisher and attack it to book.
				System.out.println("Publisher: " + publisher);
			}
			
			String publishedDate = volumeInfo.getString(JSON_PUBLISHED_DATE);
			try {
				if (publishedDate.length() > 4) {
					Calendar c = Calendar.getInstance();
					c.setTime(new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH).parse(publishedDate));
					book.setReleaseYear(new Date(c.get(Calendar.SECOND) * 1000));
				} else {
					Calendar c = Calendar.getInstance();
					c.setTime(new SimpleDateFormat("yyyy", Locale.ENGLISH).parse(publishedDate));
					book.setReleaseYear(new Date(c.get(Calendar.SECOND) * 1000));
				}
				
				System.out.println("Published Date: " + book.getReleaseYear());
				
			} catch (ParseException e) {
				//do nothing
			}
			
			if (volumeInfo.has(JSON_DESCRIPTION)) {
				String desc = volumeInfo.getString(JSON_DESCRIPTION);
				book.setDescription(desc);
				System.out.println("Description: " + desc);
			}
			
			if (volumeInfo.has(JSON_PAGE_COUNT)) {
				
				int pageCount = volumeInfo.getInt(JSON_PAGE_COUNT);
				book.setNumOfPages(pageCount);
				
				System.out.println("Page Count = " + pageCount);
			}
			
			if (volumeInfo.has(JSON_CATEGORIES)) {
				
				JSONArray categoriesArray = volumeInfo.getJSONArray(JSON_CATEGORIES);
				int categories_count = categoriesArray.length();
				for (int i = 0; i < categories_count; i++) {
					//check with db if category exists.
					//if yes, then if book has category, do nothing.
					//        otherwise, attach category to book.
					//if no, create new category and attach it to book.
					System.out.println("Category: " + categoriesArray.getString(i));
				}
			}
			
			if (volumeInfo.has(JSON_AVG_RATING)) {
				
				double rating = volumeInfo.getDouble(JSON_AVG_RATING);
				book.setGoogleRating(rating);
				
				System.out.println("Google Rating: " + rating);
			}
			
			if (volumeInfo.has(JSON_IMAGRE_LINKS)) {
				String thumbnailURL = volumeInfo.getJSONObject(JSON_IMAGRE_LINKS).getString(JSON_THUMBNAIL);
				book.setThumbnailURL(new URL(thumbnailURL));
				System.out.println("Thumbnail: " + thumbnailURL);
			}
			
			if (volumeInfo.has(JSON_LANGUAGE)) {
				String lang = volumeInfo.getString(JSON_LANGUAGE);
				book.setLanguage(lang);
				System.out.println("Language: " + lang);
			}
			
		} catch (JSONException e) {
			e.printStackTrace();
			//do nothing, null will be returned
		}
		
		return book;
		
	}
	
}
